// JavaScript Document
    (function() {

    // Localize jQuery variable
    var jQuery;

    log("Version of jQuery which is already loaded: " + window.jQuery.fn.jquery);
    /******** Load jQuery if not present *********/
    if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.2') {
        log("Dynamically loading jQuery");
        var script_tag = document.createElement('script');
        script_tag.setAttribute("type","text/javascript");
        script_tag.setAttribute("src",
            "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
        if (script_tag.readyState) {
          script_tag.onreadystatechange = function () { // For old versions of IE
              if (this.readyState == 'complete' || this.readyState == 'loaded') {
                  scriptLoadHandler();
              }
          };
        } else { // Other browsers
          script_tag.onload = scriptLoadHandler;
        }
        // Try to find the head, otherwise default to the documentElement
        (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
        log("jQuery script tag created");
    } else {
        log("jQuery already loaded");
        // The jQuery version on the window is the one we want to use
        jQuery = window.jQuery;
        main();
    }

    /******** Called once jQuery has loaded ******/
    function scriptLoadHandler() {
        log("Before noConflict window.jQuery version is " + window.jQuery.fn.jquery);
        // Restore $ and window.jQuery to their previous values and store the
        // new jQuery in our local jQuery variable
        jQuery = window.jQuery.noConflict(true);
        log("After noConflict window.jQuery version is back " + window.jQuery.fn.jquery);
        main(); // Run main() once jQuery has loaded
    }

    /******** Our main function ********/
    function main() { 
      jQuery(document).ready(function($) { 
          log("jQuery version " + $.fn.jquery + " has been loaded.");
          log("Executing our main function.");
          log("Retrieving content of h1 element using jQuery: " +
              $('h2').text());
      });
    }
    })(); // We call our anonymous function immediately
    setTimeout(function(){
        log("JQuery version " + jQuery.fn.jquery + " is still available")
    }, 1000);
	
    function log(s) { 
        var log_container = document.getElementById('log-container');
        if (log_container) {
          var text = document.createTextNode(s);
          var br = document.createElement("br");
          log_container.appendChild(text);
          log_container.appendChild(br);
        }
    }